﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using DC2010;
using DC2010.Objects;
using DC2010.Objects.Data;

namespace RolePlaying.Events
{
    public class EventsPlayerPanel
    {
        public EventsPlayerPanel()
        {
            RM.Get("PlayerPanel1_Hand_Right").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel1_OnMouseLeft);
            RM.Get("PlayerPanel1_Hand_Left").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel1_OnMouseLeft);

            RM.Get("PlayerPanel2_Hand_Right").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel2_OnMouseLeft);
            RM.Get("PlayerPanel2_Hand_Left").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel2_OnMouseLeft);

            RM.Get("PlayerPanel3_Hand_Right").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel3_OnMouseLeft);
            RM.Get("PlayerPanel3_Hand_Left").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel3_OnMouseLeft);

            RM.Get("PlayerPanel4_Hand_Right").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel4_OnMouseLeft);
            RM.Get("PlayerPanel4_Hand_Left").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsPlayerPanel4_OnMouseLeft);

            RM.Get("PlayerPanel1").OnMouseRight += new ActiveRectangle.mouseStateDelegate(RM_OnPlayerPanelMouseRight);
            RM.Get("PlayerPanel2").OnMouseRight += new ActiveRectangle.mouseStateDelegate(RM_OnPlayerPanelMouseRight);
            RM.Get("PlayerPanel3").OnMouseRight += new ActiveRectangle.mouseStateDelegate(RM_OnPlayerPanelMouseRight);
            RM.Get("PlayerPanel4").OnMouseRight += new ActiveRectangle.mouseStateDelegate(RM_OnPlayerPanelMouseRight);

            RM.Get("PlayerPanel1Stats").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(PlayerPanel1Stats_OnMouseLeft);
            RM.Get("PlayerPanel2Stats").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(PlayerPanel2Stats_OnMouseLeft);
            RM.Get("PlayerPanel3Stats").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(PlayerPanel3Stats_OnMouseLeft);
            RM.Get("PlayerPanel4Stats").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(PlayerPanel4Stats_OnMouseLeft);

        }

        void EventsPlayerPanel1_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandleMouseLeft(1, ar);
        }

        void EventsPlayerPanel2_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandleMouseLeft(2, ar);
        }

        void EventsPlayerPanel3_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandleMouseLeft(3, ar);
        }

        void EventsPlayerPanel4_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandleMouseLeft(4,ar);
        }

        public void HandleMouseLeft(int memberNumber, ActiveRectangle ar)
        {
            //if (Root.I.GameSystem.SelectedMember != memberNumber)//hands are available...
            //{
            //    if (ar.Name == "PlayerPanel" + memberNumber + "_Hand_Right")
            //    {
            //        Creature c = Root.I.GameSystem.Party.Info.MembersBrowsable[memberNumber];
            //        c.Info.ActionOptionsAreDisplayed_Right = false;
            //        Root.I.GameSystem.SwitchItem("Inventory_Body_1", c.Info.Equip.Items);
            //        return;
            //    }
            //    if (ar.Name == "PlayerPanel" + memberNumber + "_Hand_Left")
            //    {
            //        Creature c = Root.I.GameSystem.Party.Info.MembersBrowsable[memberNumber];
            //        c.Info.ActionOptionsAreDisplayed_Left = false;
            //        Root.I.GameSystem.SwitchItem("Inventory_Body_2", c.Info.Equip.Items);
            //        return;
            //    }
            //}


        }

        void RM_OnPlayerPanelMouseRight(MouseState state, ActiveRectangle ar)
        {
            switch (ar.Name)
            {
                case "PlayerPanel1":
                    {
                        Root.I.GameSystem.SelectedMember = Root.I.GameSystem.Party.GetCreatureByIndex(1);
                        Root.I.GameSystem.CurrentScreen = "Inventory";
                        break;
                    }
                case "PlayerPanel2":
                    {
                        Root.I.GameSystem.SelectedMember = Root.I.GameSystem.Party.GetCreatureByIndex(2);
                        Root.I.GameSystem.CurrentScreen = "Inventory";
                        break;
                    }
                case "PlayerPanel3":
                    {
                        Root.I.GameSystem.SelectedMember = Root.I.GameSystem.Party.GetCreatureByIndex(3);
                        Root.I.GameSystem.CurrentScreen = "Inventory";
                        break;
                    }
                case "PlayerPanel4":
                    {
                        Root.I.GameSystem.SelectedMember = Root.I.GameSystem.Party.GetCreatureByIndex(4);
                        Root.I.GameSystem.CurrentScreen = "Inventory";
                        break;
                    }
            }
        }





        void PlayerPanel1Stats_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandlePPMouseLeft(1, ar);
        }

        void PlayerPanel2Stats_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandlePPMouseLeft(2, ar);
        }

        void PlayerPanel3Stats_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandlePPMouseLeft(3, ar);
        }

        void PlayerPanel4Stats_OnMouseLeft(MouseState state, ActiveRectangle ar)
        {
            HandlePPMouseLeft(4, ar);
        }

        void HandlePPMouseLeft(int memberNumber, ActiveRectangle ar)
        {
            CreatureData c = Root.I.GameSystem.SelectedMember;//.Party.Info.MembersBrowsable[memberNumber];
            ItemData i = Root.I.GameSystem.ItemInHand;

            if (i != null)
            {
                bool bResult = c.Inventory.PutItemToFirstFreePosition(i.Id);
                if (bResult) Root.I.GameSystem.ItemInHand = null;
            }
        }
    }
}
